package cn.zxl.spring.holiday.job.web.controller;

import cn.zxl.spring.holiday.job.domain.Permission;
import cn.zxl.spring.holiday.job.domain.Role;
import cn.zxl.spring.holiday.job.qo.QueryObject;
import cn.zxl.spring.holiday.job.service.PermissionService;
import cn.zxl.spring.holiday.job.service.RoleService;
import cn.zxl.spring.holiday.job.web.json.JsonResult;
import cn.zxl.spring.holiday.job.web.log.Log;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author bofeifei
 * @since 2021-10-01
 */
@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    @Autowired
    private PermissionService permissionService;

    @RequiresPermissions(value = {"角色展示","role:list"},logical = Logical.OR)
    @Log(title = "角色展示")
    @RequestMapping("/list")
    public String list(@ModelAttribute("qo") QueryObject qo, Model model) {
        PageInfo<Role> query = roleService.query(qo);
        model.addAttribute("pageInfo", query);
        return "role/list";
    }

    @RequiresPermissions(value = {"角色编辑页面","role:input"},logical = Logical.OR)
    @Log(title = "角色编辑页面")
    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            Role role = roleService.getById(id);
            model.addAttribute("role", role);
            List<Permission> dress = permissionService.findByRoleId(id);
            model.addAttribute("dress", dress);
        }
        List<Permission> permissions = permissionService.list(null);
        model.addAttribute("permissions", permissions);
        return "role/input";
    }

    @ResponseBody
    @RequiresPermissions(value = {"角色删除","role:delete"},logical = Logical.OR)
    @Log(title = "角色删除")
    @RequestMapping("/delete")
    public JsonResult delete(Long id) {
        JsonResult jsonResult = new JsonResult();
        try {
            if (id != null) {
                roleService.deleteForRole(id);
            }
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setSuccess(false);
            jsonResult.setMsg("请求失败");
        }
        return jsonResult;
    }

    @RequiresPermissions(value = {"角色编辑","role:saveOrUpdate"},logical = Logical.OR)
    @Log(title = "角色编辑")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Role role,Long[] permissionIds) {
        if (role.getId() != null) {
            roleService.updateRolAndPer(role,permissionIds);
            return "redirect:/role/list";
        }
        roleService.saveRolAndPer(role,permissionIds);
        return "redirect:/role/list";
    }
}

